fix: Trim DSN string before URI parsing#5113
Merged
Conversation
Trailing or leading whitespace in the DSN string (commonly introduced by copy-paste) causes a URISyntaxException that crashes the application on startup. Trim the DSN before passing it to the URI constructor. Fixes GH-5087 Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛
Internal Changes 🔧Deps
🤖 This preview updates automatically when you update the PR. |
Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| e59e22a | 329.74 ms | 383.31 ms | 53.57 ms |
| fc5ccaf | 270.49 ms | 363.90 ms | 93.41 ms |
| d15471f | 361.89 ms | 378.07 ms | 16.18 ms |
| d15471f | 322.58 ms | 396.08 ms | 73.50 ms |
| abfcc92 | 304.04 ms | 370.33 ms | 66.29 ms |
| 91bb874 | 310.68 ms | 359.24 ms | 48.56 ms |
| ab8a72d | 316.24 ms | 356.38 ms | 40.14 ms |
| d15471f | 286.65 ms | 314.68 ms | 28.03 ms |
| 6727e14 | 337.22 ms | 373.94 ms | 36.71 ms |
| f064536 | 329.00 ms | 395.62 ms | 66.62 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| e59e22a | 1.58 MiB | 2.20 MiB | 635.34 KiB |
| fc5ccaf | 1.58 MiB | 2.13 MiB | 557.54 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| abfcc92 | 1.58 MiB | 2.13 MiB | 557.31 KiB |
| 91bb874 | 1.58 MiB | 2.13 MiB | 559.07 KiB |
| ab8a72d | 1.58 MiB | 2.12 MiB | 551.55 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| 6727e14 | 1.58 MiB | 2.28 MiB | 718.64 KiB |
| f064536 | 1.58 MiB | 2.20 MiB | 633.90 KiB |
romtsn
approved these changes
Feb 26, 2026
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: Whitespace-only DSN silently disables SDK instead of erroring
- Updated
SentryOptions.setDsnto preserve whitespace-only values so initialization validates them as invalid DSNs instead of treating them as intentional empty-string disablement, and added a regression test.
- Updated
Or push these changes by commenting:
@cursor push 0a7e3bd946
Preview (0a7e3bd946)
diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java
--- a/sentry/src/main/java/io/sentry/SentryOptions.java
+++ b/sentry/src/main/java/io/sentry/SentryOptions.java
@@ -749,7 +749,12 @@
* @param dsn the DSN
*/
public void setDsn(final @Nullable String dsn) {
- this.dsn = dsn != null ? dsn.trim() : null;
+ if (dsn == null) {
+ this.dsn = null;
+ } else {
+ final String trimmedDsn = dsn.trim();
+ this.dsn = trimmedDsn.isEmpty() && !dsn.isEmpty() ? dsn : trimmedDsn;
+ }
this.parsedDsn.resetValue();
dsnHash = StringUtils.calculateStringHash(this.dsn, logger);
diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt
--- a/sentry/src/test/java/io/sentry/SentryTest.kt
+++ b/sentry/src/test/java/io/sentry/SentryTest.kt
@@ -341,6 +341,11 @@
}
@Test
+ fun `initializes Sentry with whitespace-only dsn, throwing IllegalArgumentException`() {
+ assertThrows(java.lang.IllegalArgumentException::class.java) { initForTest { it.dsn = " " } }
+ }
+
+ @Test
fun `captureUserFeedback gets forwarded to client`() {
initForTest { it.dsn = dsn }Previously an empty or whitespace-only DSN string would fall through to the URI constructor, producing a confusing error message. Now the Dsn constructor checks for empty strings after trimming and throws an IllegalArgumentException with a clear message. Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Trailing or leading whitespace in the DSN string (commonly introduced by copy-paste from dashboards, wikis, or config tools) causes a
URISyntaxExceptionthat crashes the application on startup.This trims the DSN before passing it to the
URIconstructor inDsn.java.Fixes #5087